package Dao.Impl;

import Bean.LoginBean;

import java.sql.*;

public class ManDaoImpl {

    Connection conn;
    Statement stmt;

    public LoginBean Login(LoginBean user) throws SQLException {
        String USER = "root";
        String PASSWORD = "111111";
        String URL = "jdbc:mysql://localhost:3306/java_web?&useSSL=false&serverTimezone=UTC";
        String DRIVER = "com.mysql.cj.jdbc.Driver";

        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASSWORD);

            String sql = "select * from manager";

            stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                String id = rs.getString("id");
                String password = rs.getString("password");
                if (user.getId().equals(id)&&user.getPassword().equals(password)) {
                    user.setName(rs.getString("name"));
                    return user;
                }
            }
            conn.close();
            stmt.close();
            return null;
        } catch (ClassNotFoundException e) {
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            conn.close();
            stmt.close();
        }
    }
}
